#!/bin/bash

# this is a block design of 29.5s on vs 29.5s off. The timing is
# matched to 16 TRs per block. The experiment starts with rest.
# The onset times are derived as 3.688 (TR in s) x ( 8 + N x 16 )
stim_times="1D: 29.5 88.51 147.52 206.53 265.54 324.54 383.55 442.560 501.56 650.58 619.59 678.59"
3dDeconvolve                                                                  \
    -overwrite                                                                \
    -jobs        16                                                           \
    -polort      a                                                            \
    -input       BOLD_intemp.nii.gz                                           \
    -num_stimts  1                                                            \
    -TR_times    3                                                            \
    -stim_times  1 "${stim_times}" 'UBLOCK(29.5,1)'                           \
    -stim_label  1 Task                                                       \
    -tout                                                                     \
    -x1D         MODEL_wm                                                     \
    -iresp       1 HRF_BOLD.nii.gz                                            \
    -bucket      STATS_BOLD.nii.gz

echo "GLM done" 

# Extracting activation estimated as single volumes so it can be
# opened in viewers outside of AFNI                 
3dcalc -overwrite -a STATS_BOLD.nii.gz'[1]'  -expr 'a'    -prefix beta_BOLD.nii.gz
3dcalc -overwrite -a STATS_BOLD.nii.gz'[2]'  -expr 'a'    -prefix tstat_BOLD.nii.gz  
                 
3dTstat -mean -overwrite -prefix mean.nii.gz BOLD_intemp.nii.gz 

# calculating percent signal change for BOLD based on beta and mean
3dcalc                                                                       \
    -a          mean.nii.gz                                                  \
    -b          beta_BOLD.nii.gz                                             \
    -expr       'b/a*100'                                                    \
    -prefix     beta_percent_beta_BOLD.nii.gz                                \
    -overwrite

\rm mean.nii.gz



##########################################################
######### gettign beta from GLS with VASO  ###############
##########################################################

3dDeconvolve                                                                  \
    -overwrite                                                                \
    -jobs        16                                                           \
    -polort      a                                                            \
    -input       VASO_intemp.nii.gz                                           \
    -num_stimts  1                                                            \
    -TR_times    3                                                            \
    -stim_times  1 "${stim_times}" 'UBLOCK(29.5,1)'                           \
    -stim_label  1 Task                                                       \
    -tout                                                                     \
    -x1D         MODEL_wm                                                     \
    -iresp       1 HRF_VASO.nii.gz                                            \
    -bucket      STATS_VASO.nii.gz

echo "GLM done" 

# Extracting activation estimated as single volumes so it can be
# opened in viewers outside of AFNI                 
3dcalc -overwrite -a STATS_VASO.nii.gz'[1]'  -expr '-1*a'    -prefix beta_VASO.nii.gz
3dcalc -overwrite -a STATS_VASO.nii.gz'[2]'  -expr '-1*a'    -prefix tstat_VASO.nii.gz  
                 
3dTstat -mean -overwrite -prefix mean.nii.gz VASO_intemp.nii.gz 


# calculating percent signal change for VASO based on beta and mean
3dcalc                                                                       \
    -a          mean.nii.gz                                                  \
    -b          beta_VASO.nii.gz                                        \
    -expr       'b/a*100'                                                    \
    -prefix     beta_percent_beta_VASO.nii.gz                                \
    -overwrite

\rm mean.nii.gz


cp tstat_VASO.nii.gz cond_1a.nii.gz 
cp tstat_BOLD.nii.gz cond_2a.nii.gz

